package com.gdk.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdk.service.PromotionService;
import com.gdk.service.ShopService;
import com.gdk.service.SmallticketService;
import com.gdk.utils.HUtil;
import com.gdk.utils.XVDateUtils;
import com.google.gson.Gson;

/**
 * 促销活动Controller
 * @author xuwei
 * time: 2019/02/28 13:22
 * */

@Controller
public class PromotionController {
	
	@Autowired
	PromotionService promotionService;
	@Autowired
	SmallticketService smallticketService;
	@Autowired
	ShopService shopService;
	/**
	 * 新增促销活动先查出抵扣券
	 * */
	@RequestMapping("/findAllSmallTicket.do")
	public String  findAllSmallTicket(HttpServletRequest request) {
		//查出抵扣券
		List<Map<String, Object>> findAllSmallTicket = smallticketService.doFindAllSmallTicket();
		request.setAttribute("findAllSmallTicket", findAllSmallTicket);
		//查询出所有店铺
		List<Map<String, Object>> selectAllShop = shopService.selectAllShop1();
		request.setAttribute("selectAllShop", selectAllShop);
		
		//查询出所有可选门票
		List<Map<String, Object>> allPromotionByFlag = promotionService.findAllPromotionByFlag();
		request.setAttribute("allPromotionByFlag", allPromotionByFlag);
		//查询出所有可选的优惠券
		List<Map<String, Object>> allCoupon = promotionService.findAllCouponByStatus();
		request.setAttribute("allCoupon", allCoupon);
		//查询出所有可选的抵扣卡
		
		return "jsp/promotion/promotion_add";
	}
	
	
	/**
	 * 查找所有促销活动并查出抵扣券
	 * @author xuwei
	 * time 2019/02/28
	 * */
	@RequestMapping("/findAllPromotion.do")
	public String  findAllPromotion(HttpServletRequest request) {
		System.out.println("123");
		List<Map<String, Object>> findAllPromotion = promotionService.doFindAllPromotion();
		request.setAttribute("findAllPromotion", findAllPromotion);
	
		return "jsp/promotion/promotion_manage";
	}
	
	
	/**
	 * 新增促销活动
	 * @author xuwei
	 * time 2019/02/28
	 * */
	@ResponseBody
	@RequestMapping("/addPromotion.do")
	public int  addPromotion(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		//System.out.println("map : "+map);
		int result = promotionService.doAddPromotion(map);
		System.out.println("t_id : "+map.get("t_id"));
//		int sp_id = (int) map.get("sp_id");
		if(result ==1) {
			int t_id = Integer.valueOf(map.get("t_id").toString());
			int t_flag = Integer.valueOf(map.get("t_flag").toString());
			if(t_flag == 0) {
				System.out.println("套内票");
				if(map.get("t_b_ticket") != null && !"".equals(map.get("t_b_ticket").toString())) {
					String t_b_ticket = map.get("t_b_ticket").toString();
					String[] split = t_b_ticket.split(",");
					for (String string : split) {
						int b_t_id = Integer.valueOf(string);
						
						Map<String, Object> promotionDetails = promotionService.doFindPromotionDetails(HUtil.putInMap("t_id",b_t_id));
						
						Map<String,Object> bindMap = new HashMap<String,Object>();
						
						bindMap.put("type_id", b_t_id);
						bindMap.put("t_id", t_id);
						bindMap.put("zxc_name", promotionDetails.get("t_name"));
						bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
						bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(promotionDetails.get("t_starttime").toString()));
						bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_endtime").toString()));
						bindMap.put("zxc_info", promotionDetails.get("t_info"));
						bindMap.put("zxc_count", 1);
						bindMap.put("zxc_status", promotionDetails.get("t_status"));
						bindMap.put("zxc_type", 0);
						bindMap.put("zxc_types", 0);
						bindMap.put("img", promotionDetails.get("img"));
						bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_used_time").toString()));
						promotionService.addPromotionTicketAndCouponAndCard(bindMap);
						
					}
					
				}
				if(map.get("t_b_coupon") != null && !"".equals(map.get("t_b_coupon").toString())) {
					String t_b_coupon = map.get("t_b_coupon").toString();
					String[] split = t_b_coupon.split(",");
					for (String string : split) {
						int b_coupon = Integer.valueOf(string);
						
						Map<String, Object> couponDetails = promotionService.findCouponDetails(HUtil.putInMap("c_id",b_coupon));
						
						Map<String,Object> bindMap = new HashMap<String,Object>();
						
						bindMap.put("type_id", b_coupon);
						bindMap.put("t_id", t_id);
						bindMap.put("zxc_name", couponDetails.get("c_title"));
						bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
						bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(couponDetails.get("c_btime").toString()));
						bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
						bindMap.put("zxc_info", couponDetails.get("c_title"));
						bindMap.put("zxc_count", 1);
						bindMap.put("zxc_status", couponDetails.get("c_flag"));
						bindMap.put("zxc_type", 1);
						bindMap.put("zxc_types", 0);
						bindMap.put("img", couponDetails.get("c_img"));
						bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
						promotionService.addPromotionTicketAndCouponAndCard(bindMap);
						
					}
				}
				if(map.get("t_b_card") != null && !"".equals(map.get("t_b_card").toString())) {
					String t_b_card = map.get("t_b_card").toString();
					String[] split = t_b_card.split(",");
					for (String string : split) {
						int b_card = Integer.valueOf(string);
						
						Map<String, Object> cardDetails = smallticketService.selectSmallticketDetail(HUtil.putInMap("ct_id",b_card));
						
						Map<String,Object> bindMap = new HashMap<String,Object>();
						
						bindMap.put("type_id", b_card);
						bindMap.put("t_id", t_id);
						bindMap.put("zxc_name", cardDetails.get("ct_name"));
						bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
						bindMap.put("zxc_starttime", XVDateUtils.getCurrenTimeMillis());
						bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(cardDetails.get("ct_Invalidtime").toString()));
						bindMap.put("zxc_info", cardDetails.get("ct_info"));
						bindMap.put("zxc_count", 1);
						bindMap.put("zxc_status", cardDetails.get("ct_status"));
						bindMap.put("zxc_type", 2);
						bindMap.put("zxc_types", 0);
						bindMap.put("img", cardDetails.get("ct_img"));
						bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(cardDetails.get("ct_Invalidtime").toString()));
						promotionService.addPromotionTicketAndCouponAndCard(bindMap);
					}
				
				
				}
				//可选门票判断
				int xz = Integer.valueOf(map.get("xz").toString());
				
				if(xz > 0) {
					if(map.get("t_k_ticket") != null && !"".equals(map.get("t_k_ticket").toString())) {
						String t_k_ticket = map.get("t_k_ticket").toString();
						String[] split = t_k_ticket.split(",");
						for (String string : split) {
							int k_t_id = Integer.valueOf(string);
							
							Map<String, Object> promotionDetails = promotionService.doFindPromotionDetails(HUtil.putInMap("t_id",k_t_id));
							
							Map<String,Object> bindMap = new HashMap<String,Object>();
							
							bindMap.put("type_id", k_t_id);
							bindMap.put("t_id", t_id);
							bindMap.put("zxc_name", promotionDetails.get("t_name"));
							bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
							bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(promotionDetails.get("t_starttime").toString()));
							bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_endtime").toString()));
							bindMap.put("zxc_info", promotionDetails.get("t_info"));
							bindMap.put("zxc_count", 1);
							bindMap.put("zxc_status", promotionDetails.get("t_status"));
							bindMap.put("zxc_type", 0);
							bindMap.put("zxc_types", 1);
							bindMap.put("img", promotionDetails.get("img"));
							bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_used_time").toString()));
							promotionService.addPromotionTicketAndCouponAndCard(bindMap);
							
						}
						
					}
					
					if(map.get("t_k_coupon") != null && !"".equals(map.get("t_k_coupon").toString())) {
						String t_k_coupon = map.get("t_k_coupon").toString();
						String[] split = t_k_coupon.split(",");
						for (String string : split) {
							int k_coupon_id = Integer.valueOf(string);
							
							Map<String, Object> couponDetails = promotionService.findCouponDetails(HUtil.putInMap("c_id",k_coupon_id));
							
							Map<String,Object> bindMap = new HashMap<String,Object>();

							bindMap.put("type_id", k_coupon_id);
							bindMap.put("t_id", t_id);
							bindMap.put("zxc_name", couponDetails.get("c_title"));
							bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
							bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(couponDetails.get("c_btime").toString()));
							bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
							bindMap.put("zxc_info", couponDetails.get("c_title"));
							bindMap.put("zxc_count", 1);
							bindMap.put("zxc_status", couponDetails.get("c_flag"));
							bindMap.put("zxc_type", 1);
							bindMap.put("zxc_types", 1);
							bindMap.put("img", couponDetails.get("c_img"));
							bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
							promotionService.addPromotionTicketAndCouponAndCard(bindMap);
							
						}
						
					}
				}
				
			}
			
			
			/*String checkbox_smallTicket = (String) map.get("checkbox_smallTicket");
			int[] intArr =null;
			if((!(checkbox_smallTicket == null || "".equals(checkbox_smallTicket))) && checkbox_smallTicket.contains(",")) {
				String[] valueArr = checkbox_smallTicket.split(",");
				intArr = new int[valueArr.length];
				for (int i = 0; i < valueArr.length; i++) {
					int ct_id = Integer.parseInt(valueArr[i]);
					map.put("ct_id", ct_id);
					
					promotionService.doBindPromotionAndSmallTicket(map);
				}
				
			}else if(!(checkbox_smallTicket == null || "".equals(checkbox_smallTicket))) {
				int ct_id = Integer.parseInt((String) map.get("checkbox_smallTicket"));
				map.put("ct_id", ct_id);
				promotionService.doBindPromotionAndSmallTicket(map);
			}*/
			return 1;
		}
		
		//return 0;
		return result;
	}
	/**
	 * 更改促销活动状态
	 * @author xuwei
	 * time 2019/02/28
	 * */
	@ResponseBody
	@RequestMapping("/updatePromotionStatus.do")
	public int  updatePromotionStatus(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("map  : "+map);
		
		return promotionService.doUpdatePromotionStatus(map);
	}

	/**
	 * 查看促销活动详情
	 * @author xuwei
	 * time 2019/02/28 15:08
	 * */
	@RequestMapping("/findPromotionDetails.do")
	public String findPromotionDetails(@RequestParam Map<String,Object> map,HttpServletRequest request) {
		Map<String, Object> findPromotionDetails = promotionService.doFindPromotionDetails(map);
		request.setAttribute("promotionDetails",findPromotionDetails);
		//查出所有抵扣券
		List<Map<String, Object>> findAllSmallTicket = smallticketService.doFindAllSmallTicket();
		request.setAttribute("findAllSmallTicket", findAllSmallTicket);
		//查询出已绑定的抵扣券
		List<Map<String, Object>> findBindPromotionAndSmallTicket = promotionService.doFindBindPromotionAndSmallTicket(map);
		request.setAttribute("findBindPromotionAndSmallTicket", findBindPromotionAndSmallTicket);
		System.out.println("已绑定  :  "+findBindPromotionAndSmallTicket);
				
		
		//取消绑定前先确认此活动是否已开始
		
		List<Map<String, Object>> findOrderByTid = promotionService.doFindOrderByTid(map);
		int order_flag = 0;
		if(findOrderByTid.size() > 0) {
			order_flag = 1;
		}
		
		request.setAttribute("order_flag", order_flag);
		//查询出所有店铺
			List<Map<String, Object>> selectAllShop = shopService.selectAllShop1();
			request.setAttribute("selectAllShop", selectAllShop);
			
			
			//查询出所有可选门票
			List<Map<String, Object>> allPromotionByFlag = promotionService.findAllPromotionByFlag();
			request.setAttribute("allPromotionByFlag", allPromotionByFlag);	
			//查询出所有可选的优惠券
			List<Map<String, Object>> allCoupon = promotionService.findAllCouponByStatus();
			request.setAttribute("allCoupon", allCoupon);
			System.out.println(" ;; "+findPromotionDetails);
			System.out.println(" ;; "+findPromotionDetails.get("t_flag").toString());
		//查询商品绑定的套票
		int t_flag = Integer.valueOf(findPromotionDetails.get("t_flag").toString());
		if(t_flag == 0) {
			List<Map<String, Object>> ticketBind = promotionService.findTicketBind(map);
			
			List<Map<String, Object>> t_b_list = new ArrayList<Map<String, Object>>(); 
			List<Map<String, Object>> t_k_list = new ArrayList<Map<String, Object>>(); 
			//查询商品绑定的优惠券
			List<Map<String, Object>> t_b_coupon_list = new ArrayList<Map<String, Object>>(); 
			List<Map<String, Object>> t_k_coupon_list = new ArrayList<Map<String, Object>>(); 
			//查询商品绑定的抵扣卡
			List<Map<String, Object>> t_b_card_list = new ArrayList<Map<String, Object>>(); 
			List<Map<String, Object>> t_k_card_list = new ArrayList<Map<String, Object>>(); 
			
			for (Map<String, Object> map2 : ticketBind) {
				int zxc_types = Integer.valueOf(map2.get("zxc_types").toString());
				int zxc_type = Integer.valueOf(map2.get("zxc_type").toString());
				if(zxc_types == 0 && zxc_type == 0) {
					t_b_list.add(map2);
					
				}
				if(zxc_types == 1 && zxc_type == 0) {
					t_k_list.add(map2);
				}
				
				if(zxc_types == 0 && zxc_type == 1) {
					t_b_coupon_list.add(map2);
				}
				
				if(zxc_types == 1 && zxc_type == 1) {
					t_k_coupon_list.add(map2);
				}
				
				if(zxc_types == 0 && zxc_type == 2) {
					t_b_card_list.add(map2);
				}
				if(zxc_types == 1 && zxc_type == 2) {
					t_k_card_list.add(map2);
				}
			}
			
			request.setAttribute("t_b_list", t_b_list);
			request.setAttribute("t_k_list", t_k_list);
			request.setAttribute("t_b_coupon_list", t_b_coupon_list);
			request.setAttribute("t_k_coupon_list", t_k_coupon_list);
			request.setAttribute("t_b_card_list", t_b_card_list);
			request.setAttribute("t_k_card_list", t_k_card_list);
			
			
			
		}	
			
			
		return "jsp/promotion/promotion_update";		
	}
	
	/**
	 * 修改活动信息
	 * @author xuwei
	 * time 2019/02/28 15:08
	 * */
	@ResponseBody
	@RequestMapping("/updatePromotionDetails.do")
	public int updatePromotionDetails(@RequestParam Map<String, Object> map,HttpServletRequest request) {
		

	//	System.out.println("参数："+map);
	//	System.out.println(CommonUtils.StripHT(map.get("t_desc").toString()));
		//下面进行文章的添加操作
		int result = promotionService.doUpdatePromotionDetails(map);
		
		
		
		if(result ==1) {
	/*		String checkbox_smallTicket = (String) map.get("checkbox_smallTicket");
			//System.out.println("checkbox_smallTicket  : "+checkbox_smallTicket);
			int[] intArr =null;
			if((!(checkbox_smallTicket == null || "".equals(checkbox_smallTicket))) && checkbox_smallTicket.contains(",")) {
			//	System.out.println("有逗号");
				String[] valueArr = checkbox_smallTicket.split(",");
				intArr = new int[valueArr.length];
				//清空绑定
				promotionService.doDelPromotionAndTicket(map);
				for (int i = 0; i < valueArr.length; i++) {
					int ct_id = Integer.parseInt(valueArr[i]);
					map.put("ct_id", ct_id);
					
					//绑定
					promotionService.doBindPromotionAndSmallTicket(map);
				}
				
			}else if(!(checkbox_smallTicket == null || "".equals(checkbox_smallTicket))) {
			//	System.out.println("无逗号");
				int ct_id = Integer.parseInt((String) map.get("checkbox_smallTicket"));
				map.put("ct_id", ct_id);
				//清空绑定
				promotionService.doDelPromotionAndTicket(map);
				//绑定
				promotionService.doBindPromotionAndSmallTicket(map);
			}else {
				//System.out.println("啥都没有");
				//清空绑定
				promotionService.doDelPromotionAndTicket(map);
			}
			*/

				//清空绑定,重新绑定
			System.out.println(map.get("t_b_ticket"));
			
			
			Map<String,Object> delMap = new HashMap<String,Object>();
			delMap.put("t_id", map.get("t_id"));
			delMap.put("zxc_type", 0);
			delMap.put("zxc_types", 0);
			
			promotionService.delPromotionTicketAndType(delMap);
			if(map.get("t_b_ticket") != null && !"".equals(map.get("t_b_ticket").toString())) {
						String t_b_ticket = map.get("t_b_ticket").toString();
						String[] split = t_b_ticket.split(",");
						
						for (String string : split) {
							int b_t_id = Integer.valueOf(string);
							Map<String, Object> promotionDetails = promotionService.doFindPromotionDetails(HUtil.putInMap("t_id",b_t_id));
							
							Map<String,Object> bindMap = new HashMap<String,Object>();
							
							bindMap.put("type_id", b_t_id);
							bindMap.put("t_id",map.get("t_id") );
							bindMap.put("zxc_name", promotionDetails.get("t_name"));
							bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
							String t_starttime = promotionDetails.get("t_starttime").toString();
							bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(t_starttime));
							bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_endtime").toString()));
							bindMap.put("zxc_info", promotionDetails.get("t_info"));
							bindMap.put("zxc_count",1);
							bindMap.put("zxc_status", promotionDetails.get("t_status"));
							bindMap.put("zxc_type", 0);
							bindMap.put("zxc_types", 0);
							bindMap.put("img", promotionDetails.get("img"));
							bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_used_time").toString()));
							promotionService.addPromotionTicketAndCouponAndCard(bindMap);
						}
				
					
				}
			
			
			delMap.put("zxc_type", 1);
			delMap.put("zxc_types", 0);
			promotionService.delPromotionTicketAndType(delMap);
			if(map.get("t_b_coupon") != null && !"".equals(map.get("t_b_coupon").toString())) {
				
				String t_b_coupon = map.get("t_b_coupon").toString();
				String[] split = t_b_coupon.split(",");
				for (String string : split) {
					int b_coupon = Integer.valueOf(string);
					
					Map<String, Object> couponDetails = promotionService.findCouponDetails(HUtil.putInMap("c_id",b_coupon));
					
					Map<String,Object> bindMap = new HashMap<String,Object>();
					
					bindMap.put("type_id", b_coupon);
					bindMap.put("t_id", map.get("t_id"));
					bindMap.put("zxc_name", couponDetails.get("c_title"));
					bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
					bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(couponDetails.get("c_btime").toString()));
					bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
					bindMap.put("zxc_info", couponDetails.get("c_title"));
					bindMap.put("zxc_count", 1);
					bindMap.put("zxc_status", couponDetails.get("c_flag"));
					bindMap.put("zxc_type", 1);
					bindMap.put("zxc_types", 0);
					bindMap.put("img", couponDetails.get("c_img"));
					bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
					promotionService.addPromotionTicketAndCouponAndCard(bindMap);
					
				}
			}
			
			delMap.put("zxc_type", 2);
			delMap.put("zxc_types", 0);
			promotionService.delPromotionTicketAndType(delMap);
			
			if(map.get("t_b_card") != null && !"".equals(map.get("t_b_card").toString())) {
				String t_b_card = map.get("t_b_card").toString();
				String[] split = t_b_card.split(",");
				for (String string : split) {
					int b_card = Integer.valueOf(string);
					
					Map<String, Object> cardDetails = smallticketService.selectSmallticketDetail(HUtil.putInMap("ct_id",b_card));
					
					Map<String,Object> bindMap = new HashMap<String,Object>();
					
					bindMap.put("type_id", b_card);
					bindMap.put("t_id", map.get("t_id"));
					bindMap.put("zxc_name", cardDetails.get("ct_name"));
					bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
					bindMap.put("zxc_starttime", XVDateUtils.getCurrenTimeMillis());
					bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(cardDetails.get("ct_Invalidtime").toString()));
					bindMap.put("zxc_info", cardDetails.get("ct_info"));
					bindMap.put("zxc_count", 1);
					bindMap.put("zxc_status", cardDetails.get("ct_status"));
					bindMap.put("zxc_type", 2);
					bindMap.put("zxc_types", 0);
					bindMap.put("img", cardDetails.get("ct_img"));
					bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(cardDetails.get("ct_Invalidtime").toString()));
					promotionService.addPromotionTicketAndCouponAndCard(bindMap);
				}
			
			
			
			}
			
				int xz = Integer.valueOf(map.get("xz").toString());
				
				if(xz > 0) {
					//清空绑定
					delMap.put("zxc_type", 0);
					delMap.put("zxc_types", 1);
					promotionService.delPromotionTicketAndType(delMap);
					
					if(map.get("t_k_ticket") != null && !"".equals(map.get("t_k_ticket").toString())) {
						String t_k_ticket = map.get("t_k_ticket").toString();
						System.out.println("t_k_ticket : "+t_k_ticket);
						String[] split = t_k_ticket.split(",");
						
						for (String string : split) {
							int k_t_id = Integer.valueOf(string);
							Map<String, Object> promotionDetails = promotionService.doFindPromotionDetails(HUtil.putInMap("t_id",k_t_id));
							
							Map<String,Object> bindMap = new HashMap<String,Object>();
							
							bindMap.put("type_id", k_t_id);
							bindMap.put("t_id",map.get("t_id") );
							bindMap.put("zxc_name", promotionDetails.get("t_name"));
							bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
							String t_starttime = promotionDetails.get("t_starttime").toString();
							bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(t_starttime));
							bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_endtime").toString()));
							bindMap.put("zxc_info", promotionDetails.get("t_info"));
							bindMap.put("zxc_count",1);
							bindMap.put("zxc_status", promotionDetails.get("t_status"));
							bindMap.put("zxc_type", 0);
							bindMap.put("zxc_types", 1);
							bindMap.put("img", promotionDetails.get("img"));
							bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(promotionDetails.get("t_used_time").toString()));
						
							System.out.println("bindMap : "+bindMap);
							promotionService.addPromotionTicketAndCouponAndCard(bindMap);
						}
				
					
					}
					delMap.put("zxc_type", 1);
					delMap.put("zxc_types", 1);
					promotionService.delPromotionTicketAndType(delMap);
					if(map.get("t_k_coupon") != null && !"".equals(map.get("t_k_coupon").toString())) {
						String t_k_coupon = map.get("t_k_coupon").toString();
						String[] split = t_k_coupon.split(",");
						for (String string : split) {
							int k_coupon_id = Integer.valueOf(string);
							
							Map<String, Object> couponDetails = promotionService.findCouponDetails(HUtil.putInMap("c_id",k_coupon_id));
							
							Map<String,Object> bindMap = new HashMap<String,Object>();

							bindMap.put("type_id", k_coupon_id);
							bindMap.put("t_id", map.get("t_id"));
							bindMap.put("zxc_name", couponDetails.get("c_title"));
							bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
							bindMap.put("zxc_starttime", XVDateUtils.stringDateToMillis(couponDetails.get("c_btime").toString()));
							bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
							bindMap.put("zxc_info", couponDetails.get("c_title"));
							bindMap.put("zxc_count", 1);
							bindMap.put("zxc_status", couponDetails.get("c_flag"));
							bindMap.put("zxc_type", 1);
							bindMap.put("zxc_types", 1);
							bindMap.put("img", couponDetails.get("c_img"));
							bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(couponDetails.get("c_etime").toString()));
							promotionService.addPromotionTicketAndCouponAndCard(bindMap);
						}
					}
					
					
					delMap.put("zxc_type", 2);
					delMap.put("zxc_types", 1);
					promotionService.delPromotionTicketAndType(delMap);
					
					if(map.get("t_k_card") != null && !"".equals(map.get("t_k_card").toString())) {
						String t_k_card = map.get("t_k_card").toString();
						String[] split = t_k_card.split(",");
						for (String string : split) {
							int k_card = Integer.valueOf(string);
							
							Map<String, Object> cardDetails = smallticketService.selectSmallticketDetail(HUtil.putInMap("ct_id",k_card));
							
							Map<String,Object> bindMap = new HashMap<String,Object>();
							
							bindMap.put("type_id", k_card);
							bindMap.put("t_id", map.get("t_id"));
							bindMap.put("zxc_name", cardDetails.get("ct_name"));
							bindMap.put("zxc_createtime", XVDateUtils.getCurrenTimeMillis());
							bindMap.put("zxc_starttime", XVDateUtils.getCurrenTimeMillis());
							bindMap.put("zxc_endtime",  XVDateUtils.stringDateToMillis(cardDetails.get("ct_Invalidtime").toString()));
							bindMap.put("zxc_info", cardDetails.get("ct_info"));
							bindMap.put("zxc_count", 1);
							bindMap.put("zxc_status", cardDetails.get("ct_status"));
							bindMap.put("zxc_type", 2);
							bindMap.put("zxc_types", 1);
							bindMap.put("img", cardDetails.get("ct_img"));
							bindMap.put("t_used_time",  XVDateUtils.stringDateToMillis(cardDetails.get("ct_Invalidtime").toString()));
							promotionService.addPromotionTicketAndCouponAndCard(bindMap);
						}
					
					
					
					}
				}else {
					System.out.println("delMap : "+delMap);
					delMap.put("zxc_types", 1);
					delMap.remove("zxc_type");
					promotionService.delPromotionTicketAndType(delMap);
				}
			
			}
			
			
			
		
		
		
		
		
		
		
	//	System.out.println("result  : "+result);
		return result;		
	}
	
	
	
	
	/**
	 * 查询所有状态正常的门票
	 * */	
	@RequestMapping("/toShoppingTicket.do")
	public String  toShoppingTicket(HttpServletRequest request) {
		Map<String,Object> root = (Map<String, Object>) request.getSession().getAttribute("root");
		if(root == null) {
			return "jsp/login";
		}
		
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("t_status",0);
		List<Map<String, Object>> selectAllTicket = promotionService.selectAllTicket(map);
		/*[{
			id: 1234567876,
			imgUrl: 'img/3.jpg',
			goodsInfo: '号地块健身房回复的科技示范户快速坚实的看了看大家发快递了很费劲的开始放假',
			goodsParams: '四季度后付款的酸辣粉',
			price: 199,
			goodsCount: 0,
			singleGoodsMoney: 199
		}*/
		Map<String,Object> reMap = new HashMap<String,Object>();
		
	/*	{
			  "code": 0
			  ,"msg": ""
			  ,"count": 3000000
			  ,"data": [{
			    "id": "10001"
			    ,"username": "杜甫"
			    ,"email": "xianxin@layui.com"
			    ,"sex": "男"
			    ,"city": "浙江杭州"
			    ,"sign": "点击此处，显示更多。当内容超出时，点击单元格会自动显示更多内容。"
			    ,"experience": "116"
			    ,"ip": "192.168.0.8"
			    ,"logins": "108"
			    ,"joinTime": "2016-10-14"
			  }]
					  }*/
		
		List<Map<String, Object>> allTicket = new ArrayList<Map<String,Object>>();
		
		for (Map<String, Object> map2 : selectAllTicket) {
			Map<String,Object> ticket = new HashMap<String,Object>();
			ticket.put("id", map2.get("t_id"));
			ticket.put("imgUrl", map2.get("t_show"));
			ticket.put("goodsInfo", map2.get("t_name"));
			ticket.put("goodsParams", map2.get("t_jj"));
			ticket.put("price", map2.get("t_sales_price"));
			ticket.put("goodsCount", 1);
			ticket.put("singleGoodsMoney", map2.get("t_sales_price"));
			ticket.put("t_status", "正常");
			allTicket.add(ticket);
		}
		
		
		//Gson gson = new Gson();
		//String allTicket_json = gson.toJson(allTicket);
		
		
		request.setAttribute("allTicket", allTicket);
		
		
		/*reMap.put("code", 0);
		reMap.put("msg","");
		reMap.put("count",allTicket.size());
		reMap.put("data",allTicket);
		Gson gson = new Gson();
		String allTicket_json = gson.toJson(reMap);
		*/
		return "jsp/ticket/ticket-manage";
	}
	
	/**
	 * 查询所有状态正常的门票
	 * */	
	@ResponseBody
	@RequestMapping("/selectAllTicket.do")
	public String  selectAllTicket(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("t_status",0);
		List<Map<String, Object>> selectAllTicket = promotionService.selectAllTicket(map);
	
		Map<String,Object> reMap = new HashMap<String,Object>();
		
		/*	{
				  "code": 0
				  ,"msg": ""
				  ,"count": 3000000
				  ,"data": [{
				    "id": "10001"
				    ,"username": "杜甫"
				    ,"email": "xianxin@layui.com"
				    ,"sex": "男"
				    ,"city": "浙江杭州"
				    ,"sign": "点击此处，显示更多。当内容超出时，点击单元格会自动显示更多内容。"
				    ,"experience": "116"
				    ,"ip": "192.168.0.8"
				    ,"logins": "108"
				    ,"joinTime": "2016-10-14"
				  }]
						  }*/
			
			List<Map<String, Object>> allTicket = new ArrayList<Map<String,Object>>();
			
			for (Map<String, Object> map2 : selectAllTicket) {
				Map<String,Object> ticket = new HashMap<String,Object>();
				ticket.put("id", map2.get("t_id"));
				ticket.put("imgUrl", map2.get("t_show"));
				ticket.put("goodsInfo", map2.get("t_name"));
				ticket.put("goodsParams", map2.get("t_jj"));
				ticket.put("price", map2.get("t_sales_price"));
				ticket.put("goodsCount", 1);
				ticket.put("singleGoodsMoney", map2.get("t_sales_price"));
				ticket.put("t_status", "正常");
				allTicket.add(ticket);
			}
			
			
			//Gson gson = new Gson();
			//String allTicket_json = gson.toJson(allTicket);
			
			
			request.setAttribute("allTicket", allTicket);
			
			
			reMap.put("code", 0);
			reMap.put("msg","");
			reMap.put("count",allTicket.size());
			reMap.put("data",allTicket);
			Gson gson = new Gson();
			String allTicket_json = gson.toJson(reMap);
			
			return allTicket_json;
	}
	
	
	
	/**
	 * 新增报名活动前查出抵扣券
	 * */
	@RequestMapping("/preApplyfindAllSmallTicket.do")
	public String  preApplyfindAllSmallTicket(HttpServletRequest request) {
		//查出抵扣券
		List<Map<String, Object>> findAllSmallTicket = smallticketService.doFindAllSmallTicket();
		request.setAttribute("findAllSmallTicket", findAllSmallTicket);
		//查询出所有店铺
		List<Map<String, Object>> selectAllShop = shopService.selectAllShop1();
		request.setAttribute("selectAllShop", selectAllShop);
		
		//查询出所有可选门票
		List<Map<String, Object>> allPromotionByFlag = promotionService.findAllPromotionByFlag();
		request.setAttribute("allPromotionByFlag", allPromotionByFlag);
		//查询出所有可选的优惠券
		List<Map<String, Object>> allCoupon = promotionService.findAllCouponByStatus();
		request.setAttribute("allCoupon", allCoupon);
		//查询出所有可选的抵扣卡
		
		return "jsp/promotion/promotion_apply_add";
	}
	
	/**
	 * 查看报名促销活动详情
	 * @author xuwei
	 * time 2019/02/28 15:08
	 * */
	@RequestMapping("/applyFindPromotionDetails.do")
	public String applyFindPromotionDetails(@RequestParam Map<String,Object> map,HttpServletRequest request) {
		Map<String, Object> findPromotionDetails = promotionService.doFindPromotionDetails(map);
		request.setAttribute("promotionDetails",findPromotionDetails);
		//查出所有抵扣券
		List<Map<String, Object>> findAllSmallTicket = smallticketService.doFindAllSmallTicket();
		request.setAttribute("findAllSmallTicket", findAllSmallTicket);
		//查询出已绑定的抵扣券
		List<Map<String, Object>> findBindPromotionAndSmallTicket = promotionService.doFindBindPromotionAndSmallTicket(map);
		request.setAttribute("findBindPromotionAndSmallTicket", findBindPromotionAndSmallTicket);
		System.out.println("已绑定  :  "+findBindPromotionAndSmallTicket);
				
		
		//取消绑定前先确认此活动是否已开始
		
		List<Map<String, Object>> findOrderByTid = promotionService.doFindOrderByTid(map);
		int order_flag = 0;
		if(findOrderByTid.size() > 0) {
			order_flag = 1;
		}
		
		request.setAttribute("order_flag", order_flag);
		//查询出所有店铺
			List<Map<String, Object>> selectAllShop = shopService.selectAllShop1();
			request.setAttribute("selectAllShop", selectAllShop);
			
			
			//查询出所有可选门票
			List<Map<String, Object>> allPromotionByFlag = promotionService.findAllPromotionByFlag();
			request.setAttribute("allPromotionByFlag", allPromotionByFlag);	
			//查询出所有可选的优惠券
			List<Map<String, Object>> allCoupon = promotionService.findAllCouponByStatus();
			request.setAttribute("allCoupon", allCoupon);
			System.out.println(" ;; "+findPromotionDetails);
			System.out.println(" ;; "+findPromotionDetails.get("t_flag").toString());
		//查询商品绑定的套票
		int t_flag = Integer.valueOf(findPromotionDetails.get("t_flag").toString());
		if(t_flag == 0) {
			List<Map<String, Object>> ticketBind = promotionService.findTicketBind(map);
			
			List<Map<String, Object>> t_b_list = new ArrayList<Map<String, Object>>(); 
			List<Map<String, Object>> t_k_list = new ArrayList<Map<String, Object>>(); 
			//查询商品绑定的优惠券
			List<Map<String, Object>> t_b_coupon_list = new ArrayList<Map<String, Object>>(); 
			List<Map<String, Object>> t_k_coupon_list = new ArrayList<Map<String, Object>>(); 
			//查询商品绑定的抵扣卡
			List<Map<String, Object>> t_b_card_list = new ArrayList<Map<String, Object>>(); 
			List<Map<String, Object>> t_k_card_list = new ArrayList<Map<String, Object>>(); 
			
			for (Map<String, Object> map2 : ticketBind) {
				int zxc_types = Integer.valueOf(map2.get("zxc_types").toString());
				int zxc_type = Integer.valueOf(map2.get("zxc_type").toString());
				if(zxc_types == 0 && zxc_type == 0) {
					t_b_list.add(map2);
					
				}
				if(zxc_types == 1 && zxc_type == 0) {
					t_k_list.add(map2);
				}
				
				if(zxc_types == 0 && zxc_type == 1) {
					t_b_coupon_list.add(map2);
				}
				
				if(zxc_types == 1 && zxc_type == 1) {
					t_k_coupon_list.add(map2);
				}
				
				if(zxc_types == 0 && zxc_type == 2) {
					t_b_card_list.add(map2);
				}
				if(zxc_types == 1 && zxc_type == 2) {
					t_k_card_list.add(map2);
				}
			}
			
			request.setAttribute("t_b_list", t_b_list);
			request.setAttribute("t_k_list", t_k_list);
			request.setAttribute("t_b_coupon_list", t_b_coupon_list);
			request.setAttribute("t_k_coupon_list", t_k_coupon_list);
			request.setAttribute("t_b_card_list", t_b_card_list);
			request.setAttribute("t_k_card_list", t_k_card_list);
			
			
			
		}	
			
			
		return "jsp/promotion/promotion_apply_update";		
	}


	/**
	 * 查询活动报名
	 * */
	@RequestMapping("/selectApplyUserManager.do")
	public String selectApplyUserManager(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allApply = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = promotionService.selectApplyUserManager(map).size();
		
		map.put("allApplyItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/promotion/apply_user_manage";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allApply = promotionService.selectApplyUserManager(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
		//Gson gson = new Gson();
		for (Map<String, Object> map2 : allApply) {
		
			if(map2.get("addtime") != null) {
				int p = Integer.valueOf(map2.get("addtime").toString());
				if(p != 0) {
					map2.put("addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			//Map<String,Object> sp = new HashMap<String,Object>();
			//规格
			/*if(map2.get("o_gg") != null && !"".equals(map2.get("o_gg").toString())) {
				sp = gson.fromJson(map2.get("o_gg").toString(), sp.getClass());
			}
			map2.put("sp", sp);*/
		}
		
		
		
		request.setAttribute("allApply",allApply);
		return "jsp/promotion/apply_user_manage";
	}

}